package com.slz3.business.module.wmsManagement.wmsStockDetailItems.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsStockDetailItems.domain.WmsStockDetailItems;
import com.slz3.business.module.wmsManagement.wmsStockDetailItems.domain.WmsStockDetailItemsFactory;
import com.slz3.business.module.wmsManagement.wmsStockDetailItems.steward.WmsStockDetailItemsSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class WmsStockDetailItemsServiceImpl implements WmsStockDetailItemsService {
    @Override
    public WmsStockDetailItems save(WmsStockDetailItems wmsStockDetailItems) {
        return Repositorys.wmsStockDetailItems.save(WmsStockDetailItemsFactory.initBaseFields(wmsStockDetailItems));
    }
    @Override
    public Page<WmsStockDetailItems> page(MultiValueMap<String, String> params) {
        Page<WmsStockDetailItems> wmsStockDetailItemsPage = Repositorys.wmsStockDetailItems.findAll(
                WmsStockDetailItemsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsStockDetailItemsPage;
    }


    @Override
    public WmsStockDetailItems modify(WmsStockDetailItems wmsStockDetailItemsNew) {
        return Repositorys.wmsStockDetailItems.saveAndFlush(WmsStockDetailItemsFactory.initBaseFields(wmsStockDetailItemsNew));
    }

    @Override
    public Optional<WmsStockDetailItems> single(UUID uuid) {
        return Repositorys.wmsStockDetailItems.findById(uuid);
    }

    @Override
    public List<WmsStockDetailItems> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsStockDetailItems.findAllById(uuids);
    }

    @Override
    public List<WmsStockDetailItems> delete(List<WmsStockDetailItems> wmsStockDetailItemsList) {
        wmsStockDetailItemsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsStockDetailItemsFactory.initBaseFields(item);
        });
        return Services.wmsStockDetailItemsTransactional.batchModify(wmsStockDetailItemsList);
    }

}
